package com.rmxx.fruit.activity;

import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.company.android.fruit.R;
import com.kwan.xframe.common.adatper.CommonFragmentPageAdapter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;
import com.rmxx.fruit.fragment.HomeFragment;
import com.rmxx.fruit.fragment.MineFragment;
import com.rmxx.fruit.fragment.ShopCarFragment;
import com.rmxx.fruit.fragment.ShopFragment;

public class MainActivity extends BaseCommonActivity implements ViewPager.OnPageChangeListener {

	public ViewPager vp_content;
	private CommonFragmentPageAdapter mPageAdapter;
	private View ll_tab_home, ll_tab_shop, ll_tab_shopcar, ll_tab_mine;
	private ImageView iv_tab_home, iv_tab_shop, iv_tab_shopcar, iv_tab_mine;
	private TextView tv_tab_home, tv_tab_shop, tv_tab_shopcar, tv_tab_mine;

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_main;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected BasePresenter getBasePresenter() {
		return null;
	}


	@Override
	protected void initViews() {
		super.initViews();
		vp_content = findViewById(R.id.vp_content);

		ll_tab_home = findViewById(R.id.tab_home);
		ll_tab_shop = findViewById(R.id.tab_shop);
		ll_tab_shopcar = findViewById(R.id.tab_shopcar);
		ll_tab_mine = findViewById(R.id.tab_mine);

		iv_tab_home = findViewById(R.id.iv_tab_home);
		iv_tab_shop = findViewById(R.id.iv_tab_shop);
		iv_tab_shopcar = findViewById(R.id.iv_tab_shopcar);
		iv_tab_mine = findViewById(R.id.iv_tab_mine);

		tv_tab_home = findViewById(R.id.tv_tab_home);
		tv_tab_shop = findViewById(R.id.tv_tab_shop);
		tv_tab_shopcar = findViewById(R.id.tv_tab_shopcar);
		tv_tab_mine = findViewById(R.id.tv_tab_mine);
	}

	@Override
	protected void initViewSetting() {
		super.initViewSetting();

		ll_tab_home.setOnClickListener(this);
		ll_tab_shop.setOnClickListener(this);
		ll_tab_shopcar.setOnClickListener(this);
		ll_tab_mine.setOnClickListener(this);

		mPageAdapter = new CommonFragmentPageAdapter(getSupportFragmentManager());

		mPageAdapter.addFragment(HomeFragment.newInstance(), "in");
		mPageAdapter.addFragment(ShopFragment.newInstance(), "shai");
		mPageAdapter.addFragment(ShopCarFragment.newInstance(), "buy");
		mPageAdapter.addFragment(MineFragment.newInstance(), "me");
		//ViewPager.PageTransformer transformer = new DepthPageTransformer();
		//vp_content.setPageTransformer(true, transformer);
		vp_content.addOnPageChangeListener(this);
		vp_content.setAdapter(mPageAdapter);

		onPageSelected(0);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected String getTitleTxt() {
		return null;
	}

	@Override
	public String getPageName() {
		return null;
	}

	@Override
	public void onPageScrolled(int i, float v, int i1) {

	}

	@Override
	public void onPageSelected(int i) {

		switch (i) {
			case 0:

				tv_tab_home.setTextColor(getResources().getColor(R.color.mainRed));
				tv_tab_shop.setTextColor(Color.parseColor("#999999"));
				tv_tab_shopcar.setTextColor(Color.parseColor("#999999"));
				tv_tab_mine.setTextColor(Color.parseColor("#999999"));

				iv_tab_home.setSelected(true);
				iv_tab_shop.setSelected(false);
				iv_tab_shopcar.setSelected(false);
				iv_tab_mine.setSelected(false);
				break;
			case 1:

				tv_tab_shop.setTextColor(getResources().getColor(R.color.mainRed));
				tv_tab_home.setTextColor(Color.parseColor("#999999"));
				tv_tab_shopcar.setTextColor(Color.parseColor("#999999"));
				tv_tab_mine.setTextColor(Color.parseColor("#999999"));

				iv_tab_home.setSelected(false);
				iv_tab_shop.setSelected(true);
				iv_tab_shopcar.setSelected(false);
				iv_tab_mine.setSelected(false);
				break;
			case 2:
				tv_tab_shopcar.setTextColor(getResources().getColor(R.color.mainRed));
				tv_tab_shop.setTextColor(Color.parseColor("#999999"));
				tv_tab_home.setTextColor(Color.parseColor("#999999"));
				tv_tab_mine.setTextColor(Color.parseColor("#999999"));

				iv_tab_home.setSelected(false);
				iv_tab_shop.setSelected(false);
				iv_tab_shopcar.setSelected(true);
				iv_tab_mine.setSelected(false);
				break;
			case 3:
				tv_tab_mine.setTextColor(getResources().getColor(R.color.mainRed));
				tv_tab_shop.setTextColor(Color.parseColor("#999999"));
				tv_tab_shopcar.setTextColor(Color.parseColor("#999999"));
				tv_tab_home.setTextColor(Color.parseColor("#999999"));

				iv_tab_home.setSelected(false);
				iv_tab_shop.setSelected(false);
				iv_tab_shopcar.setSelected(false);
				iv_tab_mine.setSelected(true);
				break;
		}

	}

	@Override
	public void onPageScrollStateChanged(int i) {

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);

		if (v == ll_tab_home) {
			vp_content.setCurrentItem(0);
		} else if (v == ll_tab_shop) {
			vp_content.setCurrentItem(1);
		} else if (v == ll_tab_shopcar) {
			vp_content.setCurrentItem(2);
		} else if (v == ll_tab_mine) {
			vp_content.setCurrentItem(3);
		}
	}

	private boolean isPressedBackOnce = false;
	private long firstPressedTime = 0;
	private long secondPressedTime = 0;

	@Override
	public void onBackPressed() {
		if (isPressedBackOnce) {
            // 说明已经按了一次 这是第二次
			secondPressedTime = System.currentTimeMillis();
			if (secondPressedTime - firstPressedTime > 2000) {
                // 第一次点击作废了，重新计算
				toastMsg("再点一次退出");
				isPressedBackOnce = true;
				firstPressedTime = System.currentTimeMillis();
			} else {
                //说明两秒之内点击的第二次
				finish();
                // 状态回复至初
				isPressedBackOnce = false;
				firstPressedTime = 0;
				secondPressedTime = 0;
			}
		} else {
            // 说明第一次
			toastMsg("再点一次退出");
			isPressedBackOnce = true;
			firstPressedTime = System.currentTimeMillis();
		}
	}

}
